import {Api} from './api';
import { Conversion } from './method';

let userLoginRegist = document.querySelectorAll('.user-back'),
    url = window.location.href.split("?")[1];

switch (url){
    case "regist":
        document.querySelector(".judge-login").style.display = "none";
        break;
    case "login":
        document.querySelector(".judge-regist").style.display = "none";
        break;
}

userLoginRegist.forEach((value,index,array)=>{
    value.onclick = ()=>{
        let data = {username:document.querySelector(".username").value,password:document.querySelector(".password").value};
        let select = Conversion.loginEnglish(value.innerText),token = "";
        let cookies=document.cookie.split("; ");
        cookies.forEach((value,index,array)=>{
            if(value.split("=")[0] === "web"){
                token = value.split("=")[1]
            }
        });
        switch (select){
            case "login":
                Api.userPostHttp(select,data,token).then((res)=>{
                    window.location.href = `../index.html`;
                }).catch((err)=>{
                    window.location.href = `../index.html`;
                    console.log(err);
                });
                break;
            case "regist":
                Api.userGetHttp(select,data).then(res=>{

                }).catch(res=>{
                    console.log(res)
                });
                break;
            case "back":
                window.history.back();
                break;
        }
    }
});